package com.ohy.tencent.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("mg")
public class ManGuoVideoController {

    @GetMapping(value = "getClass")
    public String getAllClass() {
        String url = "https://pianku.api.mgtv.com/rider/channel-list-config?platform=msite";
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getSearch")
    public String getSearch(@RequestParam(value = "word") String word) throws UnsupportedEncodingException {
        String url = " https://mobileso.bz.mgtv.com/msite/suggest/v1?q=" + URLEncoder.encode(word, "UTF-8");
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getAllVideo")
    public String getAllVideo(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                              @RequestParam(value = "kind", required = false, defaultValue = "a4") String kind,
                              @RequestParam(value = "chargeInfo", required = false, defaultValue = "a1") String chargeInfo,
                              @RequestParam(value = "edition", required = false, defaultValue = "a7") String edition,
                              @RequestParam(value = "sort", required = false, defaultValue = "c2") String sort
    ) {
        String url = "https://pianku.api.mgtv.com/rider/list/msite?abroad=0&_support=10000000&fstlvlId=3&ic=3&pc=30&hudong=1" +
                "&kind=" + kind +
                "&chargeInfo=" + chargeInfo +
                "&edition=" + edition +
                "&sort=" + sort +
                "&pn=" + page;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getAllTV")
    public String getAllTV(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                           @RequestParam(value = "kind", required = false, defaultValue = "a4") String kind,
                           @RequestParam(value = "chargeInfo", required = false, defaultValue = "a1") String chargeInfo,
                           @RequestParam(value = "area", required = false, defaultValue = "a3") String area,
                           @RequestParam(value = "sort", required = false, defaultValue = "c2") String sort
    ) {
        String url = "https://pianku.api.mgtv.com/rider/list/msite?abroad=0&_support=10000000&fstlvlId=2&ic=2&pc=30&hudong=1" +
                "&kind=" + kind +
                "&chargeInfo=" + chargeInfo +
                "&area=" + area +
                "&sort=" + sort +
                "&pn=" + page;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getVideoInfo")
    public String getVideoInfo(@RequestParam(value = "partId") String partId, @RequestParam(value = "clipId") String clipId) {
        String url = "https://v5m.api.mgtv.com/remaster/vrs/getVideoListByPartId?abroad=0&partId=" + partId + "&clipId=" + clipId + "&pageNum=1&pageSize=2000&needLocate=0&_support=10000000";
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getSearchRes")
    public String getSearchRes(@RequestParam(value = "word") String word) throws UnsupportedEncodingException {
        String url = "https://mobileso.bz.mgtv.com/msite/search/v2?q=" + URLEncoder.encode(word, "UTF-8");
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        JSONObject jsonObject = JSONObject.parseObject(execute.body());
        JSONArray dataArray = jsonObject.getJSONObject("data").getJSONArray("contents");
        List<JSONObject> list = new ArrayList<>();
        for (int i = 0; i < dataArray.size(); i++) {
            if (!dataArray.getJSONObject(i).toString().contains("source\":\"imgo\"")) continue;
            if (dataArray.getJSONObject(i).toString().contains("text\":\"预告")) continue;
            String title = Jsoup.parse(dataArray.getJSONObject(i).getJSONArray("data").getJSONObject(0).getString("title")).text();
            url =  dataArray.getJSONObject(i).getJSONArray("data").getJSONObject(0).getString("url");
            String img = dataArray.getJSONObject(i).getJSONArray("data").getJSONObject(0).getString("img");
            JSONObject object = new JSONObject();
            object.put("title", title);
            object.put("pic", img);
            object.put("url", "https://m.mgtv.com" +url);
            object.put("type", "mv");
            if (i != (dataArray.size() - 1) && dataArray.getJSONObject(i + 1).toString().contains("单行集数")){
                object.put("type", "tv");
                object.put("partId", url.substring(url.lastIndexOf("/")+1,url.indexOf(".")));
                object.put("clipId", url.substring(url.indexOf("/b/")+"/b/".length(),url.lastIndexOf("/")));
            }
            list.add(object);
        }
        return list.toString();
    }
}
